﻿using Architecture.CrossCutting.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel.Configuration;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.CrossCutting.Web.WCF
{
    public class PipelineDiagnosticClientBehavior : BehaviorExtensionElement
    {

        const String FULLLOG_PROPERTYNAME = "Full";
        const String BEFORESTEP_PROPERTYNAME = "LogBeforeStep";
        const String AFTERSTEP_PROPERTYNAME = "LogAfterStep";
        const String ENABLED_PROPERTYNAME = "Enabled";


        public override Type BehaviorType
        {
            get { return typeof(PipelineDiagnosticClient); }
        }

        protected override object CreateBehavior()
        {
            if (this.Enabled)
            {
                var pipelineDiagnostic = CustomContext.Current.DependencyResolver.GetService<PipelineDiagnosticClient>();
                pipelineDiagnostic.IsFullLog = this.FullLog;
                pipelineDiagnostic.IsEnabled = this.Enabled;
                pipelineDiagnostic.LogAfterStep = this.LogAfterStep;
                pipelineDiagnostic.LogBeforeStep = this.LogBeforeStep;
                return pipelineDiagnostic;
            }
            else
            {
                return null;
            }
        }


        [ConfigurationProperty(ENABLED_PROPERTYNAME, DefaultValue = true, IsRequired = false)]
        public Boolean Enabled
        {
            get
            {
                return (Boolean)base[ENABLED_PROPERTYNAME];
            }
            set
            {
                base[ENABLED_PROPERTYNAME] = value;
            }
        }



        [ConfigurationProperty(FULLLOG_PROPERTYNAME, DefaultValue = false, IsRequired = false)]
        public Boolean FullLog
        {
            get
            {
                return (Boolean)base[FULLLOG_PROPERTYNAME];
            }
            set
            {
                base[FULLLOG_PROPERTYNAME] = value;
            }
        }

        [ConfigurationProperty(BEFORESTEP_PROPERTYNAME, DefaultValue = false, IsRequired = false)]
        public Boolean LogBeforeStep
        {
            get
            {
                return (Boolean)base[BEFORESTEP_PROPERTYNAME];
            }
            set
            {
                base[BEFORESTEP_PROPERTYNAME] = value;
            }
        }

        [ConfigurationProperty(AFTERSTEP_PROPERTYNAME, DefaultValue = false, IsRequired = false)]
        public Boolean LogAfterStep
        {
            get
            {
                return (Boolean)base[AFTERSTEP_PROPERTYNAME];
            }
            set
            {
                base[AFTERSTEP_PROPERTYNAME] = value;
            }
        }
    }
}
